function formatTime(date) {
  date = toDate(date);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatTime3(date) {
  date = toDate(date);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatTime4(date) {
  date = toDate(date);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

function formatDate(date) {
  return toDate(date).getTime();
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getDate(date, space) {
  date = toDate(date);
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  return year + (space || '-') + formatNumber(month) + (space || '-') + formatNumber(day);
}

function getTime(date) {
  date = toDate(date);
  var hour = date.getHours();
  var minute = date.getMinutes();
  return formatNumber(hour) + ':' + formatNumber(minute);
}

function timeToDate(time) {
  return new Date(time);
}

function getWeek(date) {
  date = toDate(date);
  var weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  return weekday[date.getDay()];
}

function subRemark(str, length) {
  var length = length || 10;
  if (str.length > length) {
    return str.substring(0, length) + '...';
  } else {
    return str
  }
}

function toDate(date) {
  if (isNaN(date)) {
    return new Date(date.replace(/\-/g, "/"));
  } else {
    return new Date(date);
  }
}

// 本周上周开始结束时间
function getBeforeWeek(n) {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var date = now.getDate();
  var day = now.getDay();
  if (day !== 0) {
    n = n + (day - 1);
  }
  else {
    n = n + day;
  }
  if (day) {
    if (month > 1) {
      month = month;
    }
    else {
      year = year - 1;
      month = 12;
    }
  }
  now.setDate(now.getDate() - n);
  year = now.getFullYear();
  month = now.getMonth() + 1;
  date = now.getDate();
  var s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
  return s;
}

function Rad(d) {
  return d * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
}
//计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
function GetDistance(lat1, lng1, lat2, lng2) {

  var radLat1 = Rad(lat1);
  var radLat2 = Rad(lat2);
  var a = radLat1 - radLat2;
  var b = Rad(lng1) - Rad(lng2);
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
    Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137; // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000; //输出为公里
  //s=s.toFixed(4);
  return s;
}

/**
 * 时间格式化
 * dateStr： 要格式化的时间
 * type: 类型 1 年月日； 2 / 分隔 ；3  . 分隔 
 * showTime:是否显示时分
 * showDiff:是否显示几分钟前，几小时前
 */
function itemDateToShow(dateStr, type, showTime, showDiff) {
  var minuteUnit = 1000 * 60,
    hourUnit = minuteUnit * 60,
    dayUnit = hourUnit * 24,
    halfamonthUnit = dayUnit * 15,
    monthUnit = dayUnit * 30;
  var itemDate = toDate(dateStr);
  var now = new Date().getTime();
  var diffValue = now - itemDate.getTime();
  if (diffValue < 0) {
    return showDate(dateStr, type, showTime);
  }
  var monthC = diffValue / monthUnit,
    weekC = diffValue / (7 * dayUnit),
    dayC = diffValue / dayUnit,
    hourC = diffValue / hourUnit,
    minC = diffValue / minuteUnit;
  var result = "";
  if (showDiff) {
    if (monthC >= 1) {
      result = showDate(dateStr, type, showTime);
    } else if (weekC >= 1) {
      result = parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
      result = parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
      result = parseInt(hourC) + "个小时前";
    } else if (minC >= 1) {
      result = parseInt(minC) + "分钟前";
    } else {
      result = "刚刚";
    }
  } else {
    result = showDate(dateStr, type, showTime);
  }
  return result;
}

function showDate(dateStr, type, showTime) {
  var result = "";
  var itemDate = toDate(dateStr);
  var year = itemDate.getFullYear();
  var month = itemDate.getMonth() + 1;
  var day = itemDate.getDate();
  var hour = itemDate.getHours();
  var minute = itemDate.getMinutes();
  result += year;
  result += (1 == type ? "年" : (2 == type ? "/" : "."));
  result += (month < 10 ? "0" + month : month);
  result += (1 == type ? "月" : (2 == type ? "/" : "."));
  result += (day < 10 ? "0" + day : day);
  result += (1 == type ? "日" : "");
  if (showTime) {
    result += " ";
    result += (hour < 10 ? "0" + hour : hour);
    result += ":";
    result += (minute < 10 ? "0" + minute : minute);
  }
  return result;
}

function isEmojiCharacter(substring) {
  for (var i = 0; i < substring.length; i++) {
    var hs = substring.charCodeAt(i);
    if (0xd800 <= hs && hs <= 0xdbff) {
      if (substring.length > 1) {
        var ls = substring.charCodeAt(i + 1);
        var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
        if (0x1d000 <= uc && uc <= 0x1f77f) {
          return true;
        }
      }
    } else if (substring.length > 1) {
      var ls = substring.charCodeAt(i + 1);
      if (ls == 0x20e3) {
        return true;
      }
    } else {
      if (0x2100 <= hs && hs <= 0x27ff) {
        return true;
      } else if (0x2B05 <= hs && hs <= 0x2b07) {
        return true;
      } else if (0x2934 <= hs && hs <= 0x2935) {
        return true;
      } else if (0x3297 <= hs && hs <= 0x3299) {
        return true;
      } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 ||
        hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b ||
        hs == 0x2b50) {
        return true;
      }
    }
  }
}

Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1,                 //月份 
    "d+": this.getDate(),                    //日 
    "h+": this.getHours(),                   //小时 
    "m+": this.getMinutes(),                 //分 
    "s+": this.getSeconds(),                 //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds()             //毫秒 
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}

module.exports = {
  formatTime: formatTime,
  formatTime3: formatTime3,
  formatTime4: formatTime4,
  formatDate: formatDate,
  getDate: getDate,
  toDate: toDate,
  timeToDate: timeToDate,
  getTime: getTime,
  getWeek: getWeek,
  subRemark: subRemark,
  itemDateToShow: itemDateToShow,
  GetDistance: GetDistance,
  isEmojiCharacter: isEmojiCharacter,
  getBeforeWeek: getBeforeWeek,
}